<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="开课编号" prop="courseId">
          <el-input v-model="dataForm.courseId" placeholder="开课编号"></el-input>
      </el-form-item>
          <el-form-item label="计划类型" prop="planType">
          <el-input v-model="dataForm.planType" placeholder="计划类型"></el-input>
      </el-form-item>
          <el-form-item label="开课学院" prop="courseCollege">
          <el-input v-model="dataForm.courseCollege" placeholder="开课学院"></el-input>
      </el-form-item>
          <el-form-item label="课程类别" prop="courseType">
          <el-input v-model="dataForm.courseType" placeholder="课程类别"></el-input>
      </el-form-item>
          <el-form-item label="课程分类" prop="courseClass">
          <el-input v-model="dataForm.courseClass" placeholder="课程分类"></el-input>
      </el-form-item>
          <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="dataForm.courseName" placeholder="课程名称"></el-input>
      </el-form-item>
          <el-form-item label="教学班名称" prop="className">
          <el-input v-model="dataForm.className" placeholder="教学班名称"></el-input>
      </el-form-item>
          <el-form-item label="总学时" prop="timeSum">
          <el-input v-model="dataForm.timeSum" placeholder="总学时"></el-input>
      </el-form-item>
          <el-form-item label="理论学时" prop="timeTheory">
          <el-input v-model="dataForm.timeTheory" placeholder="理论学时"></el-input>
      </el-form-item>
          <el-form-item label="实践学时" prop="praticeTime">
          <el-input v-model="dataForm.praticeTime" placeholder="实践学时"></el-input>
      </el-form-item>
          <el-form-item label="学分" prop="credit">
          <el-input v-model="dataForm.credit" placeholder="学分"></el-input>
      </el-form-item>
          <el-form-item label="选课人数" prop="chooseNum">
          <el-input v-model="dataForm.chooseNum" placeholder="选课人数"></el-input>
      </el-form-item>
          <el-form-item label="开课校区" prop="courseCampus">
          <el-input v-model="dataForm.courseCampus" placeholder="开课校区"></el-input>
      </el-form-item>
          <el-form-item label="任课教师" prop="courseTeacher">
          <el-input v-model="dataForm.courseTeacher" placeholder="任课教师"></el-input>
      </el-form-item>
          <el-form-item label="专业" prop="major">
          <el-input v-model="dataForm.major" placeholder="专业"></el-input>
      </el-form-item>
          <el-form-item label="情况说明" prop="conditions">
          <el-input v-model="dataForm.conditions" placeholder="情况说明"></el-input>
      </el-form-item>
          <el-form-item label="负责督导" prop="dutyDudao">
          <el-input v-model="dataForm.dutyDudao" placeholder="负责督导"></el-input>
      </el-form-item>
          <el-form-item label="任课教师编号" prop="teacherId">
          <el-input v-model="dataForm.teacherId" placeholder="任课教师编号"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="comment">
          <el-input v-model="dataForm.comment" placeholder="备注"></el-input>
      </el-form-item>
          <el-form-item label="课表信息" prop="courseInformation">
          <el-input v-model="dataForm.courseInformation" placeholder="课表信息"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        courseId: '',
        planType: '',
        courseCollege: '',
        courseType: '',
        courseClass: '',
        courseName: '',
        className: '',
        timeSum: '',
        timeTheory: '',
        praticeTime: '',
        credit: '',
        chooseNum: '',
        courseCampus: '',
        courseTeacher: '',
        major: '',
        conditions: '',
        dutyDudao: '',
        teacherId: '',
        comment: '',
        courseInformation: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        courseId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        planType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        courseCollege: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        courseType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        courseClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        courseName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        className: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        timeSum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        timeTheory: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        praticeTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        credit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        chooseNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        courseCampus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        courseTeacher: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        major: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        conditions: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dutyDudao: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        teacherId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        comment: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        courseInformation: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/course/dudaoarrange/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/course/dudaoarrange/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
